package com.txzt.gaea.business.plan.controller;

import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.business.plan.domain.model.PlanInfo;
import com.txzt.gaea.business.plan.domain.model.PlanPack;
import com.txzt.gaea.business.plan.domain.vo.PlanPackVo;
import com.txzt.gaea.business.plan.service.IPlanInfoService;
import com.txzt.gaea.business.plan.service.IPlanPackService;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/plan/pack")
public class PlanPackController extends BaseController {


    private static final Logger logger = LoggerFactory.getLogger(PlanPackController.class);

    @Autowired
    private IPlanPackService planPackService;
    @Autowired
    private IPlanInfoService planInfoService;



    /**
     * 分页查询
     * @param pageNumber
     * @param pageSize
     * @param planPack
     * @return
     */
    @GetMapping(value = "/list")
    public RtnInfo<?> pageList(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, PlanPack planPack) {
//        AdminUserVO adminUserVO = AdminUserContext.getUser();
//        List<Long> deptIds = deptService.getAllChildDeptIds(adminUserVO.getDeptid());
//        planPack.setDeptIds(deptIds);
        logger.info(planPack.toString());
        PageQuery<PlanPack> pageList = planPackService.pageQuery(new PageQuery<>(pageNumber, pageSize, planPack));
        pageList.getList().forEach(item -> {
            //翻译创建人
            item.setCreateName(TranslateUtil.getUserName(item.getCreateBy()));
            //翻译更新人
            item.setUpdateName(TranslateUtil.getUserName(item.getUpdateBy()));
            //翻译承担部门（离大谱）
            item.setDepartName(TranslateUtil.getDepartmentName(item.getDepartId()));
        });
        return RtnInfo.success(pageList);
    }

    /**
     * id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public RtnInfo<?> queryById(@RequestParam Long id) {
        PlanPack planPack = planPackService.selectById(id);
        PlanPackVo planPackVo = new PlanPackVo();
        BeanUtils.copyProperties(planPack, planPackVo);
        List<PlanInfo> planInfoList = planInfoService.queryByPackId(planPack.getId());
        planInfoList.forEach(p -> {
            //翻译项目组长
            if (null != p.getProjectLeader()) {
                p.setProjectLeaderName(TranslateUtil.getUserName(p.getProjectLeader()));
            }
        });
        planPackVo.setPlanInfoList(planInfoList);
        return RtnInfo.success(planPackVo);
    }

    /**
     * 新增
     * @param planPackVo
     * @return
     */
    @PostMapping(value = "/add")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> add(@RequestBody PlanPackVo planPackVo) {
        int count = planPackService.savePlanPack(planPackVo);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 编辑
     * @param planPackVo
     * @return
     */
    @PutMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> edit(@RequestBody PlanPackVo planPackVo) {
        int count = planPackService.updatePlanPack(planPackVo);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> delete(@RequestParam Long id) {
        int count = planPackService.deletePlanPack(id);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.error(500,"存在已下发的项目，不可删除！");
        }
    }
}
